﻿/*---------------------------------------------------------------
 *  iSun软件工作室
 *  
 *  用于控制T4Template模版相关数据逻辑,可用于动态生成及编译，或者生成输出相关文件
 * 
 *  作者：江怀玉
 * 
 *  日期：2012-12-27
 * 
 *  版本: V0.1
 *   
 *  修改记录:
 * 
 * 
 * 
 * ------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.Model;
using iSun.SPL;
using iSun.Common;
using iSun.BLL;
using iSun.BLL.Base;

namespace iSun.T4Template
{
    public partial class DataEntity
    {
        public static DataEntity instace = new DataEntity();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public IList<SysTableField> GetTable(string tableName)
        {
            SysTableField e = new SysTableField();
            
            return null;
        }

        /// <summary>
        /// 表的字段列表
        /// </summary>
        public IList<SysTableField> TableFieldList
        {
            get;
            set;
        }
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName
        {
            get;
            set;
        }

        /// <summary>
        /// 实体名称
        /// </summary>
        public string EntityName
        {
            get;
            set;
        }

        /// <summary>
        /// 关键字
        /// </summary>
        public string KeyField
        {
            get;
            set;
        }
    }
   

    /// <summary>
    /// 根据对象及配置的信息生成脚本信息
    /// </summary>
    public partial class Javascript
    {
        public IList<DataListTemplate> ListSet
        {
            get;
            set;
        }

        public void getListSet(string entityName)
        {
            iSun.BLL.Base.BaseBussnies bll = new BLL.Base.BaseBussnies();
            ListSet =  bll.GetEntityListByCondition<DataListTemplate>("EntityName='" + entityName + "'");
        }
    }







    /// <summary>
    /// 用于根据配置生成界面及生成相关源数据提供给界面设计器
    /// </summary>
    public partial class UIData
    {
        public static UIData Instance = new UIData();
        /// <summary>
        /// 获取配置数据信息
        /// </summary>
        /// <param name="entity">实体名（表名）</param>
        /// <returns>配置数据信息</returns>
        public IList<iSun.Model.EditUIConfig> GetUIConfig(string entity)
        {
            iSun.BLL.Base.BaseBussnies bll = new BLL.Base.BaseBussnies();
            return bll.GetEntitys<iSun.Model.EditUIConfig>("EntityName = '" + entity + "'").Where(p=>p.ControlType=="S").ToList();
        }

        /// <summary>
        /// 配置数据
        /// (可以通过外部改变数据)
        /// </summary>
        public IList<iSun.Model.EditUIConfig> EditUIConfigList
        {
            get;
            set;
        }


        /// <summary>
        /// 通过数据配置生
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public string  GetDataList(iSun.Model.EditUIConfig e)
        {
            IList<Dictionary> list = null;
            list = GetFieldDataSource(e);
            return Globals.ObjectToJSON(list);
        }

        public DataTableBaseEntity MasterEntity
        {
            get;
            set;
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public string GetSelectOption(iSun.Model.EditUIConfig e)
        {
            var list = GetFieldDataSource(e);
            if (list != null)
            {
                StringBuilder sb = new StringBuilder("");
                 sb.Append("<option value=\"\"></option>").AppendLine();
                foreach (var dic in list)
                {
                    sb.Append(string.Format("<option value=\"{0}\">{1}</option>",dic.ValueMember,dic.DisplayMember)).AppendLine();;
                }
                return sb.ToString();
            }
            return "";
        }

        public string GetSelectOption(iSun.Model.EditUIConfig e,string value)
        {
            var list = GetFieldDataSource(e);
            if (list != null)
            {
                StringBuilder sb = new StringBuilder("");
                sb.Append("<option value=\"\"></option>").AppendLine();
                foreach (var dic in list)
                {
                    if (dic.ValueMember == value)
                    {
                        sb.Append(string.Format("<option value=\"{0}\" selected>{1}</option>", dic.ValueMember, dic.DisplayMember)).AppendLine();
                    }
                    else
                    {
                        sb.Append(string.Format("<option value=\"{0}\">{1}</option>", dic.ValueMember, dic.DisplayMember)).AppendLine();
                    }
                }
                return sb.ToString();
            }
            return "";
        }
        /// <summary>
        /// 获取字段
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private IList<Dictionary> GetFieldDataSource(iSun.Model.EditUIConfig e)
        {
            IList<Dictionary> list = null;
            if (e.DataSourceType.ToLower() == "json")
            {
                list = Globals.JSonToObject<IList<Dictionary>>(e.DataSource);
            }
            if (e.DataSourceType.ToLower() == "sql")
            {
                BaseBussnies instance = new BaseBussnies();
                list = instance.GetEntitysBySqlComm<Dictionary>(e.DataSource.Replace("[B]", "'"));
            }
            if (e.DataSourceType.ToLower() == "dic")
            {
                BaseBussnies instance = new BaseBussnies();
                list = instance.GetEntitysBySqlComm<Dictionary>(string.Format("Exec [dbo].sp_GetDictionaryData '{0}'", e.DataSource));
            }
            return list;
        }

        public UIData()
        {
        }

        public UIData(string entity)
        {
            EditUIConfigList = GetUIConfig(entity);
        }

        public UIData(string entity, DataTableBaseEntity master)
        {
            EditUIConfigList = GetUIConfig(entity);
            MasterEntity = master;
        }
    }

    /// <summary>
    /// UI配置 
    /// </summary>
    public partial class UIConfig 
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public IList<iSun.Model.EditUIConfig> GetUIConfig(string entity)
        {
            iSun.BLL.Base.BaseBussnies bll = new BLL.Base.BaseBussnies();
            return bll.GetEntitys<iSun.Model.EditUIConfig>("EntityName = '" + entity + "'").OrderBy(p=>p.iOrder).ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        public IList<iSun.Model.EditUIConfig> EditUIConfigList
        {
            get;
            set;
        }

        public UIConfig(string entity)
        {
            EditUIConfigList = GetUIConfig(entity);
        }

        public  UIConfig()
        {
        }

        public UIConfig(string entity,DataTableBaseEntity master)
        {
            EditUIConfigList = GetUIConfig(entity);
            MasterEntity = master;
        }

        public DataTableBaseEntity MasterEntity
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ui"></param>
        /// <returns></returns>
        public string BulitContrl(iSun.Model.EditUIConfig ui)
        {
            string v = MasterEntity[ui.FieldName].ToString();
            switch (ui.ControlType)
            {
                case "T":
                    return string.Format(@"<label for=""{0}"">{4}</label><input type=""text"" id=""{0}"" value=""{5}""  data-dojo-type=""{2}"" name=""{0}"" required=""{1}"" />{3}", ui.FieldName, ui.IsRequire == 1, "dijit/form/TextBox", ui.IsRequire == 0 ? "(*)" : "", ui.Caption,v);
                case "N":
                   return string.Format(@"<label for=""{0}"">{4}</label><input type=""text"" id=""{0}"" name=""{0}"" value=""{5}"" data-dojo-type=""{2}"" required=""{1}""/>{3}", ui.FieldName, ui.IsRequire == 1, "dijit/form/NumberTextBox", ui.IsRequire == 0 ? "(*)" : "", ui.Caption,v);
                case "D":
                   return string.Format(@"<label for=""{0}"">{4}</label><input type=""text"" id=""{0}"" name=""{0}"" value=""{5}"" data-dojo-type=""{2}"" required=""{1}""/>{3}", ui.FieldName, ui.IsRequire == 1, "dijit/form/DateTextBox", ui.IsRequire == 0 ? "(*)" : "", ui.Caption,v);
                case "DT":
                   return string.Format(@"<label for=""{0}"">{4}</label><input type=""text"" id=""{0}"" name=""{0}""  value=""{5}""  data-dojo-type=""{2}"" required=""{1}""/>{3}", ui.FieldName, ui.IsRequire == 1, "dijit/form/TimeTextBox", ui.IsRequire == 0 ? "(*)" : "", ui.Caption,v);
                case "O":
                   return string.Format(@"<label for=""{0}"">{4}</label><input type=""text"" id=""{0}"" name=""{0}""  value=""{5}"" data-dojo-type=""{2}"" required=""{1}""/>{3}", ui.FieldName, ui.IsRequire == 1, "dijit/form/ComboBox", ui.IsRequire == 0 ? "(*)" : "", ui.Caption,v);
                case "DR":
                    return "";
                case "Z":
                    return string.Format(@"<label for=""{0}"" >{4}</label><input type=""text"" id=""{0}"" name=""{0}""  value=""{5}""  data-dojo-type=""{2}"" required=""{1}""/>{3}", ui.FieldName, ui.IsRequire == 1, "dijit/form/ComboBox", ui.IsRequire == 0 ? "(*)" : "", ui.Caption);
                case "S":
                    //return string.Format(@"<label for=""{0}"">{4}</label><input type=""text"" id=""{0}"" name=""{0}"" data-dojo-type=""{2}"" required=""{1}"" data-dojo-props=""store:{0}Store, searchAttr:'DisplayMember'""
// />{3}", ui.FieldName, ui.IsRequire == 1, "dijit/form/ComboBox", ui.IsRequire == 0 ? "(*)" : "", ui.Caption);
                    return string.Format("<label for=\"{0}\">{2}</label><select data-dojo-type=\"dijit/form/FilteringSelect\" id=\"{0}\" name=\"{0}\" class=\"cspan{3}\">{1}</select>", ui.FieldName, UIData.Instance.GetSelectOption(ui,v),ui.Caption,ui.ColSpan);
                    //UIData.Instance.GetSelectOption(ui);
                default:
                    return string.Format(@"<label for=""{0}"">{4}</label><input type=""text"" id=""{0}""  value=""{5}""   data-dojo-type=""{2}"" name=""{0}"" required=""{1}"" />{3}", ui.FieldName, ui.IsRequire == 1, "dijit/form/TextBox", ui.IsRequire == 0 ? "(*)" : "", ui.Caption ,v);
            }
        }
    }
}
